package com.sxm.yiwei.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 作者: Nade_S on 2017/11/23.
 * 网络请求助手
 */

public class NetHelper {

    private static SharedPreferences sharedPreferences;
    public static String appId;
    public static String userId;
    public static String token;
    public static String TypeJson = "application/json"; //String
    public static String TypeData = "multipart/form-data"; //file

    /**
     * 请求头参数
     * @param context 上下文
     * @param url  网址
     * @param Type 类型
     * @return
     */
    public static RequestParams getHeader(Context context,String url,String Type){

        sharedPreferences = context.getSharedPreferences("userlogin",Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        RequestParams params = new RequestParams(InternetAddressUtis.Url+url);
        params.addHeader("Content-Type",Type);
        params.addHeader("token",NetHelper.token);
        params.addHeader("appId",NetHelper.appId);
        params.addHeader("userId",NetHelper.userId);


        return params;
    }

    /**
     * x get 请求方法
     * @param params
     * @param context
     * @param from
     *
     */
    public static void XGET(RequestParams params, final Context context, final FromNetDataBack fromNetDataBack, final String from){

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject object = JSON.parseObject(result);
                String status = object.getString("status");
                if (status.equals("1")){
                    fromNetDataBack.onNetSuccess(result,from);
                }else {
                    String failure = object.getString("failure");
                    fromNetDataBack.onNetSuccessYiviErrorCode(failure,from);
                    ErrType.successErrCode(failure,context);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                fromNetDataBack.onNetError(ex,from);
                ErrType.errType(context,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    /**
     * x post 请求方法
     * @param params 请求体
     * @param context 上下文
     * @param from 请求位置
     */
    public static void XPOST(RequestParams params, final Context context, final FromNetDataBack fromNetDataBack, final String from){
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject object = JSON.parseObject(result);
                String status = object.getString("status");
                if (status.equals("1")){
                    fromNetDataBack.onNetSuccess(result,from);
                }else {
                    String failure = object.getString("failure");
                    fromNetDataBack.onNetSuccessYiviErrorCode(failure,from);
                    ErrType.successErrCode(failure,context);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                fromNetDataBack.onNetError(ex,from);
                ErrType.errType(context,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    //数据返回接口
    public interface FromNetDataBack{
        /**
         * 网络请求成功 正常返回数据
         * @param data
         * @param from
         */
        void onNetSuccess(String data, String from);

        /**
         * 网络请求成功 错误码
         * @param code
         * @param from
         */
        void onNetSuccessYiviErrorCode(String code, String from);

        /**
         * 请求失败 网络错误码
         * @param ex
         * @param from
         */
        void onNetError(Throwable ex, String from);


    }

}
